package cloud.seckill.ribbonserver.controller;

import cloud.seckill.ribbonserver.result.Result;
import cloud.seckill.ribbonserver.service.RibbonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @title: SeckillController
 * @Author Wangsj
 * @Date: 2021/5/24
 */
@Api(tags = "商品秒杀")
@RestController
@RequestMapping("/seckill")
public class SeckillController {


    @Autowired
    RibbonService ribbonService;

    @GetMapping("buy_lv1")
    @ApiOperation(value = "级别1秒杀",notes = "未加任何锁，会出现线程安全，无法实现秒杀")
    public Result buyLv1(){
        return ribbonService.getSeckillResult("buy/lv1");
    }

    @GetMapping("buy_lv2")
    @ApiOperation(value = "级别2秒杀",notes = "单机锁，出现超卖")
    public Result buyLv2(){
        return ribbonService.getSeckillResult("buy/lv2");
    }

    @GetMapping("buy_lv3")
    @ApiOperation(value = "级别3秒杀",notes = "Redis锁，使用setIfAbsent加锁,解锁为直接解锁，非原子操作，会存在问题")
    public Result buyLv3(){
        return ribbonService.getSeckillResult("buy/lv3");
    }

    @GetMapping("buy_lv4")
    @ApiOperation(value = "级别4秒杀",notes = "Redis锁，使用lua脚本解锁")
    public Result buyLv4(){
        return ribbonService.getSeckillResult("buy/lv4");
    }

    @GetMapping("buy_lv5")
    @ApiOperation(value = "级别5秒杀",notes = "Redisson锁")
    public Result buyLv5(){
        return ribbonService.getSeckillResult("buy/lv5");
    }
}
